The %p
directive in the strftime
method is used to represent the AM/PM marker in a time string. It is commonly used in
conjunction with the %I
directive, which represents the hour in a 12-hour clock.
Using the 24-hour format directive with an AM/PM marker can lead to unwanted results e.g.:
time_string = time(16,0).strftime("%H:%M %p")
print(time_string)
will print 16:00 PM
which makes no sense.
On the other hand the AM/PM marker is needed when the 12-hour format directive is used to show complete information about an hour e.g.:
time_string = time(16,0).strftime("%I:%M")
print(time_string)
will print 04:00 without indicating if the time is in the morning or the afternoon.